<?php
include_once 'IndexPresenter.php';
include_once 'AbstractView.php';
include_once 'InsultBuilder.php';
class IndexView extends AbstractView {
	private $presenter;
	private $from;
	const PREVIES_PER_PAGE = 5;
	public function __construct($from) {
		parent::__construct ();
		$this->from = $from;
		$this->presenter = new IndexPresenter ( $this, parent::getContentRepository (), parent::getUserRepository () );
	}
	public function render() {
		if (isset ( $_POST ["login"] ) && isset ( $_POST ["password"] )) {
			$this->presenter->login ( $_POST ["login"], $_POST ["password"] );
			return;
		}
		if (isset ( $_POST ["logout"] )) {
			$this->presenter->logout ();
			return;
		}
		
		require 'index.phtml';
	}
	public function logout() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "We are not gonna wait you back. Yep, seriously, dude." )->setTitle ( "Chao, looser!" )->setPicture ( "logout.jpg" )->setSource ( "index.php" );
		parent::redirect ( $builder->build () );
	}
	public function loginSucceed() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "Welcome home!" )->setTitle ( "And it is the right login & passoword!!! Hooray!!!" )->setPicture ( "login_succeed.jpg" )->setSource ( "index.php" );
		parent::redirect ( $builder->build () );
	}
	public function renderArticlePreviews() {
		if ($this->presenter->getPreviewCount () === 0) {
			echo '
				<p
				style="text-align: center; font-size: 18; color: red; vertical-align: middle; text-shadow: #000 1px 0 0px, #000 0 1px 0px, #000 -1px 0 0px, #000 0 -1px 0px;">Sorry, dude, we have no articles.</p>';
			return;
		}
		
		if ($this->from < 0 || $this->from >= $this->presenter->getPreviewCount ()) {
			$builder = new InsultBuilder ();
			$builder->setMessage ( "Only steers and queers plays with URL, and you don't much look like a steer to me so that kinda narrows it down!" )->setTitle ( "Like playing with URL?" )->setPicture ( "userprofile_user_does_not_exist.jpg" )->setSource ( "index.php" );
			parent::redirect ( $builder->build () );
			return;
		}
		
		$previews = $this->presenter->getPreviews ( $this->from, IndexView::PREVIES_PER_PAGE );
		
		foreach ( $previews as $preview ) {
			echo '<div style="padding: 5px 5px 5px 5px">';
			echo '<div style="background-color: #FFB6C1; font-size: 18px">';
			echo $preview->getTitle ();
			echo '</div>';
			echo $preview->getContent ();
			echo '<br />';
			echo ' <a href="readarticle.php?article=' . $preview->getArticleId () . '">Read more</a>';
			echo '</div>';
			echo '<br/>';
		}
	}
	public function loginError() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "Forgot password or trying to hack me?" )->setTitle ( "Blind bro?" )->setPicture ( "login_failed.png" )->setSource ( "index.php" );
		parent::redirect ( $builder->build () );
	}
	public function addAdminPanel() {
		if (parent::invokedByAdmin ()) {
			return '<a href="addarticle.php">Add an article</a>  <a href="manageusers.php">Manage users</a>';
		} else {
			return '';
		}
	}
	public function renderNavigation() {
		if ($this->presenter->getPreviewCount () === 0) {
			return;
		}
		
		echo '<div style="text-align: center"> ';
		
		$pages = 0;
		
		if ($this->presenter->getPreviewCount () % IndexView::PREVIES_PER_PAGE === 0) {
			$pages = ( int ) ($this->presenter->getPreviewCount () / IndexView::PREVIES_PER_PAGE);
		} else {
			$pages = ( int ) ($this->presenter->getPreviewCount () / IndexView::PREVIES_PER_PAGE) + 1;
		}
		
		$currentPage = ( int ) ($this->from / IndexView::PREVIES_PER_PAGE) + 1;
		
		if ($currentPage !== 1) {
			echo '<a href="index.php?from=' . ($this->from - IndexView::PREVIES_PER_PAGE) . '">Back</a>   ';
		}
		
		echo 'Page ' . $currentPage . '  of ' . $pages;
		
		if ($currentPage !== $pages) {
			echo '   <a href="index.php?from=' . ($this->from + IndexView::PREVIES_PER_PAGE) . '">Forward</a>';
		}
		
		echo '</div>';
	}
	public function getHeader() {
		$user = parent::getActiveUser ();
		if ($user != NULL) {
			$header = '
		 <form name="logoutForm" method="POST" action="">
	     <label for="logout">
	
			<div style="font-size:18; color:red; vertical-align: middle; text-shadow: #000 1px 0 0px, #000 0 1px 0px, #000 -1px 0 0px, #000 0 -1px 0px;">
					Hello,  <a  href="userprofile.php?user=' . htmlspecialchars ( $user->getUserName () ) . '">' . htmlspecialchars ( $user->getUserName () ) . '</a> </label>
		 <input type="submit" value="Log out" /> </div>
		 <input type="hidden" name="logout"/>
		 </form >';
		} else {
			$header = '
		<form name="loginForm" method="POST" action="">
		<input type="text" name="login"  placeholder="username"/>
		<input type="password" name="password"  placeholder="password"/>
		<input type="submit" value="Log in" />
		<input type="button" value="Sign up"/ onClick=window.location.href="registration.php" />
		</form>';
		}
		
		return $header;
	}
}
$from = 0;
if (isset ( $_GET ["from"] )) {
	$from = $_GET ["from"];
}
$view = new IndexView ( $from );
$view->render ();

?>